package com.dlc.yunguang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import butterknife.BindView;
import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.adapter.ProductListAdapter;
import com.dlc.yunguang.adapter.decoration.RecyclerSpace;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.BucaoCategoryBean;
import com.dlc.yunguang.net.bean.ProductListBean;
import com.dlc.yunguang.net.protocol.StockProto;
import com.dlc.yunguang.R;
import com.trello.rxlifecycle.android.ActivityEvent;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 点击某一产品分类后，查看该类下的产品规格列表
 */
public class CategoryProductListActivity extends BaseActivity {

    private static final String EXTRA_CATEGORY = "category";

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;
    private BucaoCategoryBean.DataBean mCategory;
    private ProductListAdapter<ProductListBean.DataBean> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        resolveIntent();

        mTitleBar.leftExit(this);
        mTitleBar.setTitle(getString(R.string.x_chan_list, mCategory.title));

        initRecyclerView();

        getProductList();
    }

    private void resolveIntent() {

        Intent intent = getIntent();
        mCategory = intent.getParcelableExtra(EXTRA_CATEGORY);
    }

    private void initRecyclerView() {
        mAdapter = new ProductListAdapter<>();

        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                int position) {

                ProductListBean.DataBean item = mAdapter.getItem(position);
                Intent intent = ProductStatusListActivity.newIntent(getActivity(), mCategory, item);
                startActivity(intent);
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(getResources(), 0, R.dimen.normal_24dp));
        mRecyclerView.setAdapter(mAdapter);
    }

    public void getProductList() {
        showLoadingDialog();
        StockProto.get()
            .goods(mCategory.id, null)
            .subscribeOn(Schedulers.io())
            .compose(this.<ProductListBean>bindUntilEvent(ActivityEvent.DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<ProductListBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    showToast(e.getMessage());
                    dismissLoadingDialog();
                }

                @Override
                public void onNext(ProductListBean productListBean) {
                    dismissLoadingDialog();
                    mAdapter.setNewData(productListBean.data);
                }
            });
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_product_list;
    }

    public static Intent newIntent(Activity activity, BucaoCategoryBean.DataBean category) {
        Intent intent = new Intent(activity, CategoryProductListActivity.class);
        intent.putExtra(EXTRA_CATEGORY, category);
        return intent;
    }

    //@NonNull
    //private ArrayList<ProductList> getFakeData() {
    //    ArrayList<ProductList> details = new ArrayList<>();
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 12, 12, 20, "迪尔西有限公司", "5号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 16, 16, 20, "迪尔西有限公司", "4号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    details.add(new ProductListImpl("纯棉毛巾", "毛巾", 18, 18, 20, "迪尔西有限公司", "3号仓"));
    //    return details;
    //}
}
